package com.pet.server.ext.action.square;


import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.PetAdopt;
import com.pet.server.ext.data.PetAdoptItem;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Adoptapply extends ActionBase{

	public PetAdoptItem item;
	public Adoptapply(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		item = new PetAdoptItem();
		item.adopt_id = NumberUtil.getInteger(params.getFieldValue("adopt_id"));
		item.qq = StringUtil.dealNull(params.getFieldValue("qq"));
		item.phone = StringUtil.dealNull(params.getFieldValue("phone"));
		item.description = StringUtil.dealNull(params.getFieldValue("description"));
		if(StringUtil.isEmpty(item.adopt_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		PetAdopt pa = userService.findAdoptById(item.adopt_id);
		if(null==pa){
            throw ApiError.Type.LOGIC_ERROR.toException("领养信息不存在!");			
		}
		if(userService.adoptIsApplay(item.adopt_id, user.user_id)){
			throw ApiError.Type.LOGIC_ERROR.toException("领养已经申请!");	
		}
		item.user_id=user.user_id;
		userService.savePetAdoptItem(item);
		Result result = new Result();
		result.put("massge", "申领信息提交成功!");
	    return result;
	}


}
